package net.lijingye.education.security;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by LijingYe on 2016/3/27.
 */
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        Boolean hasToken = (Boolean) httpServletRequest.getAttribute("hasToken");
        if (null == hasToken || false == hasToken) {
            httpServletResponse.getWriter().write("{\"meta\":{\"ErrCode\":\"0x000002\",\"ErrMsg\":\"Header `Authentication` not exists.\"},\"data\":null}");
        } else {
            httpServletResponse.getWriter().write("{\"meta\":{\"ErrCode\":\"0x000003\",\"ErrMsg\":\"`Access_token` not found.\"},\"data\":null}");
        }
    }
}
